package items;

import main.Map;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;

import shared.Timer;

import entities.Player;

public class Sword extends Weapon {
	private int range;
	
	public Sword(int id, int x, int y, int w, int h) throws SlickException {
		super(id, x, y, w, h);
		range = 50;
		setAttackDamage(5);
		setAttackSpeed(3);
		setAttackTimer(new Timer(1.0 / (double)getAttackSpeed()));
		setTexture("resources/items/sword.png");
	}

	@Override
	public void use(GameContainer gc, Player player) throws SlickException {
		int midX = player.getX() + player.getWidth()/2;
		int midY = player.getY();
		int tempRange;
		if(player.getDirection() == 0)
			tempRange = range;
		else
			tempRange = -range;
		
		Polygon hitArea = new Polygon(new float[] {midX, midY,
												   midX + tempRange, midY,
												   midX + tempRange, midY + getHeight(),
												   midX, midY + getHeight()});
		
		for(int i = 0; i < Map.getNPCListSize(); i++) {
			if(hitArea.intersects(Map.getNPCByIndex(i).getPolygon()) || hitArea.contains(Map.getNPCByIndex(i).getPolygon()) || Map.getNPCByIndex(i).getPolygon().contains(hitArea)) {
				if(getAttackTimer().update()) {
					Map.getNPCByIndex(i).getHit(getAttackDamage());
					getAttackTimer().resetTimer();
				}
			}
		}
	}

}
